package com.soxing.braindata.web.controller.sync;

import com.soxing.braindata.domain.response.sync.SyncResponse;
import com.soxing.braindata.domain.rpc.*;
import com.soxing.braindata.service.sync.ISyncService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.ContentCachingRequestWrapper;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 原始数据同步控制器
 */
@Slf4j
@RestController
@RequestMapping("/sync")
@Api(value = "原始数据同步控制器", tags = {"原始数据同步控制器"})
public class OriginDataSyncController {

    @Resource
    private ISyncService<OriginSubject> syncSubjectService;
    @Resource
    private ISyncService<OriginSectionRequest> syncSectionService;
    @Resource
    private ISyncService<OriginAgency> syncAgencyService;
    @Resource
    private ISyncService<OriginReserve> syncReserveService;
    @Resource
    private ISyncService<OriginSite> syncSiteService;
    @Resource
    private ISyncService<OriginUser> syncUserService;

    @PostMapping("/syncSubject")
    @ApiOperation(value = "同步课题")
    public SyncResponse syncSubject(@RequestBody OriginSubject originSubject, HttpServletRequest request) {
        ContentCachingRequestWrapper cachingRequest = new ContentCachingRequestWrapper(request);
        // 请求方法
        String method = request.getMethod();
        // URI
        String uri = request.getRequestURI();
        // 请求体
        byte[] requestContent = cachingRequest.getContentAsByteArray();
        log.info("syncSubject_Request => {} {}", method, uri);
        log.info(String.valueOf(originSubject));

        return syncSubjectService.syncData(originSubject);
    }

    @PostMapping("/syncSection")
    @ApiOperation(value = "同步方案(同步访视编码)")
    public SyncResponse syncSection(@RequestBody OriginSectionRequest originSectionRequest,HttpServletRequest request) {
        ContentCachingRequestWrapper cachingRequest = new ContentCachingRequestWrapper(request);
        // 请求方法
        String method = request.getMethod();
        // URI
        String uri = request.getRequestURI();
        // 请求体
        byte[] requestContent = cachingRequest.getContentAsByteArray();
        log.info("syncSection_Request => {} {}", method, uri);
        log.info(String.valueOf(originSectionRequest));

        return syncSectionService.syncData(originSectionRequest);
    }

    @PostMapping("/syncAgency")
    @ApiOperation(value = "同步机构")
    public SyncResponse syncAgency(@RequestBody OriginAgency originAgency,HttpServletRequest request) {
        ContentCachingRequestWrapper cachingRequest = new ContentCachingRequestWrapper(request);
        // 请求方法
        String method = request.getMethod();
        // URI
        String uri = request.getRequestURI();
        // 请求体
        byte[] requestContent = cachingRequest.getContentAsByteArray();
        log.info("syncAgency_Request => {} {}", method, uri);
        log.info(String.valueOf(originAgency));

        return syncAgencyService.syncData(originAgency);
    }

    @PostMapping("/syncSite")
    @ApiOperation(value = "同步分中心")
    public SyncResponse syncSite(@RequestBody OriginSite originSite,HttpServletRequest request) {
        ContentCachingRequestWrapper cachingRequest = new ContentCachingRequestWrapper(request);
        // 请求方法
        String method = request.getMethod();
        // URI
        String uri = request.getRequestURI();
        // 请求体
        byte[] requestContent = cachingRequest.getContentAsByteArray();
        log.info("syncSite_Request => {} {}", method, uri);
        log.info(String.valueOf(originSite));

        return syncSiteService.syncData(originSite);
    }

    @PostMapping("/syncUser")
    @ApiOperation(value = "同步人员")
    public SyncResponse syncUser(@RequestBody OriginUser originUser,HttpServletRequest request) {
        //ContentCachingRequestWrapper cachingRequest = new ContentCachingRequestWrapper(request);
        // 请求方法
        String method = request.getMethod();
        // URI
        String uri = request.getRequestURI();
        // 请求体
        //byte[] requestContent = cachingRequest.getContentAsByteArray();
        log.info("syncUser_Request => {} {}", method, uri);
        log.info(String.valueOf(originUser));

        return syncUserService.syncData(originUser);
    }

    @PostMapping("/syncReserve")
    @ApiOperation(value = "同步访视预约")
    public SyncResponse syncReserve(@RequestBody OriginReserve originReserve,HttpServletRequest request) {
        ContentCachingRequestWrapper cachingRequest = new ContentCachingRequestWrapper(request);
        // 请求方法
        String method = request.getMethod();
        // URI
        String uri = request.getRequestURI();
        // 请求体
        byte[] requestContent = cachingRequest.getContentAsByteArray();
        log.info("syncReserve_Request => {} {}", method, uri);
        log.info(String.valueOf(originReserve));

        return syncReserveService.syncData(originReserve);
    }
}
